package com.hww.v2_mtfsdk.provider;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.Log;

import com.hww.common.MyAppThis;
import com.hww.common.util.FileIOUtils;
import com.hww.common.util.FileUtils;
import com.hww.common.util.NetworkUtils;
import com.hww.common.util.SDCardUtils;
import com.hww.common.util.SharePreferencesUtils;
import com.hww.common.util.StringUtils;
import com.hww.v2_mtfsdk.Constant;

import net.facelib.akcore.AuthParameter;
import net.facelib.authkernel.db.DeviceBean;
import net.facelib.mtfsdk.V2AupsParameterProvider;

import org.json.JSONObject;


public class ProviderMtfV2Custom extends V2AupsParameterProvider/*LiveCpuIDProvider*/ {

    public ProviderMtfV2Custom() {
        String path = String.format("%s/%s.dat", SDCardUtils.getExternalFilesDir(null), getProductID());
        get().put(AuthParameter.LICENSE_FILE, path);
    }

    /**
     * 组织代码(用户组ID) 请联系工作人员获取
     */
    @Override
    public String getAuthorID() {
        String authorId = SharePreferencesUtils.getString(MyAppThis.getThis(), Constant.SHARE_LICENSE_AUTHOR_ID, "");
        if (StringUtils.isEmpty(authorId)) {
            authorId = getLicense("authorID");
        }
        return authorId;
    }

    /**
     * 产品签名 请联系工作人员获取
     */
    @Override
    public String getProductSign() {
        String productSign = SharePreferencesUtils.getString(MyAppThis.getThis(), Constant.SHARE_LICENSE_PRODUCT_SIGN, "");
        if (StringUtils.isEmpty(productSign)) {
            productSign = getLicense("productSign");
        }
        return productSign;
    }

    /**
     * 标签,用于对授权进行分类
     * 默认授权 null
     * 试用授权 "TRIAL_TAG"
     */
    @Override
    public String getAuthTag() {
        return null;
    }

    @Override
    public DeviceBean getDeviceBean() {
        DeviceBean deviceBean = new DeviceBean();
        WifiManager wifi = (WifiManager) MyAppThis.getThis().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (null != wifi) {
            if (wifi.isWifiEnabled()) {
                wifi.setWifiEnabled(true);
            }
            String ssID = wifi.getConnectionInfo().getSSID();
            deviceBean.setSsid(ssID);
        }

        deviceBean.setOs("Android-" + Build.VERSION.RELEASE);
        deviceBean.setArch(Build.CPU_ABI);
        String ip = NetworkUtils.getIPAddress(true);
        deviceBean.setIntranetIp(ip);
        deviceBean.setDeviceName(Build.PRODUCT);
        deviceBean.setModel(Build.MODEL);
        /*设备供应商*/
        deviceBean.setVendor(Build.MANUFACTURER);
        /*设备制造商*/
        deviceBean.setManufacturer("");
        Log.e("hwwFace", "V2Provider=" + deviceBean);
        return deviceBean;
    }

    /**
     * 获取授权文件
     *
     * @param key 键值
     * @return
     */
    private String getLicense(String key) {
        try {
            String fileName = "v2.txt";
            /* /storage/emulated/0  ( /sdcard )*/
            String path = SDCardUtils.getExternalStorageDirectory() + "/" + fileName;
            if (!FileUtils.isFileExists(path)) {
                /*  /storage/emulated/0/Android/data/包名/files  */
                path = SDCardUtils.getExternalFilesDir() + "/" + fileName;

                if (!FileUtils.isFileExists(path)) {
                    return "";
                }
            }
            String file2String = FileIOUtils.readFile2String(path);
            JSONObject jsonObject = new JSONObject(file2String);
            /*String authorID = jsonObject.getString("authorID");*/
            /*String productSign = jsonObject.getString("productSign");*/
            String string = jsonObject.getString(key);
            return string;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}